package setup;

import interceptor.InjectConnection;

import java.sql.Connection;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import question.*;
import utils.Var;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import account.AccountUtils;

/**
 * Created by IntelliJ IDEA.
 * User: Shawn
 * Date: 2009-6-18
 * Time: 23:21:51
 */
public class QueryAction {

  private Connection con;

  @InjectConnection
  public void setCon(Connection con) {
    this.con = con;
  }

  private List<ClassifyInfo> kmList;

  public String queryKm() throws Exception {
    HttpSession session = ServletActionContext.getRequest().getSession();
    if (!AccountUtils.hasRight(session, Var.RIGHT_SETUP)) throw new Exception("deny!");

    kmList = Classify.getClassifyList(con, QuestionVar.CLASSIFY_TYPE_KM);
    return Var.ACTION_SUCCESS;
  }

  public List<ClassifyInfo> getKmList() {
    return kmList;
  }

  private int kmID;
  private KmSettingInfo kmSettingInfo;

  public String querySetting() throws Exception {
    HttpSession session = ServletActionContext.getRequest().getSession();
    if (!AccountUtils.hasRight(session, Var.RIGHT_SETUP)) throw new Exception("deny!");

    kmSettingInfo = KmSetting.getSetting(con, kmID);
    return Var.ACTION_SUCCESS;
  }

  public void setKmID(int kmID) {
    this.kmID = kmID;
  }

  public int getKmID() {
    return kmID;
  }

  public KmSettingInfo getKmSettingInfo() {
    return kmSettingInfo;
  }

  private List<ClassifyInfo> dtList;
  private Map<Integer, List<ClassifyInfo>> ydMap;
  private Map<Integer, SetupInfo> staMap;
  private Map<Integer, SetupInfo> setMap;

  public String queryKmDetail() throws Exception {
    HttpSession session = ServletActionContext.getRequest().getSession();
    if (!AccountUtils.hasRight(session, Var.RIGHT_SETUP)) throw new Exception("deny!");

    kmSettingInfo = KmSetting.getSetting(con, kmID);
    dtList = Classify.getChildren(con, kmID);

    ydMap = new HashMap<Integer, List<ClassifyInfo>>();
    for (ClassifyInfo info : dtList) {
      List<ClassifyInfo> list = Classify.getChildren(con, info.getCid());
      ydMap.put(info.getCid(), list);
    }

    staMap = new HashMap<Integer, SetupInfo>();
    List<QuestionInfo> qList = Question.getQuestionList(con, kmID);
    for (QuestionInfo question : qList) {
      SetupInfo setupInfo = staMap.get(question.getCid());
      if (setupInfo == null) setupInfo = new SetupInfo();
      staMap.put(question.getCid(), setupInfo);

      if (question.getTx() == QuestionVar.TX_SINGLE_CHOICE) setupInfo.setSSum(setupInfo.getSSum() + 1);
      if (question.getTx() == QuestionVar.TX_MULTI_CHOICE) setupInfo.setMSum(setupInfo.getMSum() + 1);
      if (question.getTx() == QuestionVar.TX_TF) setupInfo.setTSum(setupInfo.getTSum() + 1);
    }

    setMap = new HashMap<Integer, SetupInfo>();
    List<SetupInfo> slist = Setup.getSetupList(con, kmID);
    for (SetupInfo setup : slist) setMap.put(setup.getYdID(), setup);

    return Var.ACTION_SUCCESS;
  }

  public List<ClassifyInfo> getDtList() {
    return dtList;
  }

  public Map<Integer, List<ClassifyInfo>> getYdMap() {
    return ydMap;
  }

  public Map<Integer, SetupInfo> getStaMap() {
    return staMap;
  }

  public Map<Integer, SetupInfo> getSetMap() {
    return setMap;
  }
}
